#include <openssl/hmac.h>
#include <openssl/evp.h>
#include "core/auth/shahmac1_signer.h"

namespace aliyun {

string ShaHmac1Signer::GetSignatureMethod(void) {
  return "HMAC-SHA1";
}

string ShaHmac1Signer::GetSignatureVersion(void) {
  return "1.0";
}

string ShaHmac1Signer::SignString(const string& source, const string& access_secret) {
  const EVP_MD* engine = EVP_sha1();
  HMAC_CTX ctx;
  HMAC_CTX_init(&ctx);
  HMAC_Init_ex(&ctx, (const unsigned char *)(access_secret.c_str()), access_secret.length(), engine, NULL);
  HMAC_Update(&ctx, (const unsigned char *)(source.c_str()), source.length());
  unsigned char sha1[EVP_MAX_MD_SIZE];
  unsigned int sha1_length;
  HMAC_Final(&ctx, sha1, &sha1_length);
  char base64[(EVP_MAX_MD_SIZE/3)*4];
  int base64_length = EVP_EncodeBlock((unsigned char *)(base64), sha1, sha1_length);
  return string(base64, 0, base64_length);
}

} // namespace aliyun {

// end of file
